package com.paic.mhis.hcpms.productClear.web.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.paic.mhis.hcpms.common.time.biz.service.TimeCheckService;
import com.paic.mhis.hcpms.common.time.dto.TimeCheckDTO;
import com.paic.mhis.hcpms.productClear.constants.ProductClearConstants;
import com.paic.pafa.appclient.annotation.BizServicesProxy;

@Controller
@RequestMapping("/preDupSubmitController/")
public class PreDupSubmitController {
	@BizServicesProxy(beanName = "timeCheckService")
	private TimeCheckService preDupSubmitService;

	@RequestMapping(value = "processSubmitResult.do", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> processSubmitResult(HttpServletRequest request) throws Exception {
		Map<String, Object> result = new HashMap<String, Object>();

		String startDate = request.getParameter("startDate");
		String endDate = request.getParameter("endDate");
		String code = request.getParameter("code");
		// (01为预警案件，02为稽核案件，03为稽核事件)
		List<TimeCheckDTO> checkTimeOne = this.preDupSubmitService.checkTimeOne(startDate, endDate, code);
		
		StringBuffer sql=new StringBuffer();

		if (null != checkTimeOne && checkTimeOne.size() > 0) { // 不可以使用
			
			
			String desc01=this.getWaringDec(checkTimeOne);
			String desc02=this.getAuditDec(checkTimeOne);
			String desc03=this.getAuditCaseDesc(checkTimeOne);
			if(StringUtils.hasText(desc01)){
				sql.append(desc01);
			}
			if(StringUtils.hasText(desc02)){
				sql.append(desc02);
			}
			if(StringUtils.hasText(desc03)){
				sql.append(desc03);
			}
			
			result.put("flag", false);
			result.put("codeDesc","当前时段与"+sql.toString()+"的时间段出现重叠,请核实情况避免重复扣费或少扣费。是否依然继续操作?");
			
			
		} else { // 可以使用
			result.put("flag", true);
		}
		return result;
	}



 private String getWaringDec(List<TimeCheckDTO> checkTimeOne){
	 
	 StringBuffer sql=new StringBuffer();
	 
	 List<TimeCheckDTO> list=new ArrayList<TimeCheckDTO>();
	 
	 if(checkTimeOne!=null && checkTimeOne.size()>0){
		 for(TimeCheckDTO currenBean : checkTimeOne){
			 String type=currenBean.getType();
			 if(ProductClearConstants.type_01.equals(type)){
				 list.add(currenBean);
			 }
		 }
	 }
	 
	 
	
	 if(list!=null && list.size()>0){
		 sql.append("预警案件[");
		 for(int i=0;i<list.size();i++){
			 TimeCheckDTO bean=list.get(i);
			 String type=bean.getType();
			 String code=bean.getCode();
			 if(ProductClearConstants.type_01.equals(type)){
				 sql.append(code+",");
			 }
		 }
		String currentSql=sql.substring(0,sql.length()-1);
		currentSql+="]";
		return currentSql;
	 }else{
		 return "";
	 }
	 
	 
 }
 
private String getAuditDec(List<TimeCheckDTO> checkTimeOne){
	 StringBuffer sql=new StringBuffer();
	 
   List<TimeCheckDTO> list=new ArrayList<TimeCheckDTO>();
	 
	 if(checkTimeOne!=null && checkTimeOne.size()>0){
		 for(TimeCheckDTO currenBean : checkTimeOne){
			 String type=currenBean.getType();
			 if(ProductClearConstants.type_02.equals(type)){
				 list.add(currenBean);
			 }
		 }
	 }
	 
		
	 if(list!=null && list.size()>0){
		 sql.append("稽核事件[");
		 for(int i=0;i<list.size();i++){
			 TimeCheckDTO bean=list.get(i);
			 String type=bean.getType();
			 String code=bean.getCode();
			 if(ProductClearConstants.type_02.equals(type)){
				 sql.append(code+",");
			 }
		 }
		String currentSql=sql.substring(0,sql.length()-1);
		currentSql+="]";
		return currentSql;
	 }else{
		 return "";
	 }
	 
	 
 }

private String getAuditCaseDesc(List<TimeCheckDTO> checkTimeOne){
	 StringBuffer sql=new StringBuffer();
	 
	 
    List<TimeCheckDTO> list=new ArrayList<TimeCheckDTO>();
	 
	 if(checkTimeOne!=null && checkTimeOne.size()>0){
		 for(TimeCheckDTO currenBean : checkTimeOne){
			 String type=currenBean.getType();
			 if(ProductClearConstants.type_03.equals(type)){
				 list.add(currenBean);
			 }
		 }
	 }
		
	 if(list!=null && list.size()>0){
		 sql.append("稽核案件[");
		 for(int i=0;i<list.size();i++){
			 TimeCheckDTO bean=list.get(i);
			 String type=bean.getType();
			 String code=bean.getCode();
			 if(ProductClearConstants.type_03.equals(type)){
				 sql.append(code+",");
			 }
		 }
		String currentSql=sql.substring(0,sql.length()-1);
		currentSql+="]";
		return currentSql;
	 }else{
		 return "";
	 }
	
}
	 
}
